package com.example.demo11;

import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

@Component
public class MyServletConfig implements ServletContextInitializer {

    // 读取配置文件中路由设置
    private String servlet_url="/api/*";
    // 读取配置中代理目标地址
    private String target_url="http://192.168.100.173:9997";

    @Override
    public void onStartup(ServletContext servletContext)  {


        ServletRegistration.Dynamic myServlet = servletContext.addServlet("MyServlet", MyProxy.class);

        myServlet.addMapping(servlet_url);
        myServlet.setInitParameter(ProxyServlet.P_TARGET_URI, target_url);
        myServlet.setInitParameter(ProxyServlet.P_LOG,  "true");

        ServletRegistration.Dynamic myServlet1 = servletContext.addServlet("MyServlet1", MyProxy.class);

        myServlet1.addMapping("/api1/*");
        myServlet1.setInitParameter(ProxyServlet.P_TARGET_URI, target_url);
        myServlet1.setInitParameter(ProxyServlet.P_LOG,  "true");


    }
}

